package com.example.cinemaorder.util;

import com.alibaba.fastjson2.JSON;
import com.example.cinemaorder.entity.SysMovie;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;
import java.sql.*;

/**
 * @author 高垚淼
 * @version 1.0
 */
//同步电影id、电影详情——电影排行榜页面
@Component
public class MysqlToRedis2 {
    // Redis连接设置
    String redisHost = "localhost";
    int redisPort = 6379;

    // MySQL连接设置
    String mysqlHost = "192.168.159.166";
    int mysqlPort = 33306;
    String mysqlDatabase = "cenima_system";
    String mysqlUsername = "root";
    String mysqlPassword = "123";

    // 使用@PostConstruct注解，让这个方法在类加载时执行一次
    @PostConstruct
    public void importData2() {
        try {
            // 建立MySQL连接
            String mysqlUrl = String.format("jdbc:mysql://%s:%d/%s", mysqlHost, mysqlPort, mysqlDatabase);
            Connection mysqlConn = DriverManager.getConnection(mysqlUrl, mysqlUsername, mysqlPassword);

            // 建立Redis连接
            Jedis jedis = new Jedis(redisHost, redisPort);

            // 查询MySQL数据
            String sql = "SELECT * FROM sys_movie";
            Statement statement = mysqlConn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            // 创建GenericJackson2JsonRedisSerializer对象，用于序列化和反序列化
            GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();

            // 将MySQL数据导入Redis
            while (resultSet.next()) {
                //获取对象
                String key = resultSet.getString("movie_id");
                Long movieId = resultSet.getLong("movie_id");
                String poster = resultSet.getString("movie_poster");
                String name = resultSet.getString("movie_name");
                Date date = resultSet.getDate("release_date");
                Double price = resultSet.getDouble("movie_box_office");

                //封装对象
                SysMovie sysMovie = new SysMovie();
                sysMovie.setMovieId(movieId);
                sysMovie.setMoviePoster(poster);
                sysMovie.setMovieName(name);
                sysMovie.setReleaseDate(date);
                sysMovie.setMovieBoxOffice(price);
//                ObjectMapper objectMapper = new ObjectMapper();
//                String object = objectMapper.writeValueAsString(sysMovie);

                // 使用GenericJackson2JsonRedisSerializer将对象转换为字节数组
                byte[] value = serializer.serialize(sysMovie);
                // 使用SET命令将数据存储到Redis的hash中
                jedis.set(("电影排行榜："+key).getBytes(), value);
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            mysqlConn.close();
            jedis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
